/*
 * 加载完后执行
 */
$(function () {
    const Base_Url = "http://127.0.0.1:8080/"; // TODO 配置后台API地址
    const putInStorageBut = $("#putInStorageBut");
    const addBut = $("#addBut");
    const cancelBut = $("#cancelBut");
    const searchBut = $("#searchBut");
    const pageSize = 10;
    var nowPageNum = 1;
    /**
     *设置数据
     **/
    const setTableList = (list) => {
        let html = ""
        const getTD = (v) => {
            return "<td>" + (v || "") + "</td>";
        }
        list.forEach((v, i) => {
            html += "<tr>";
            html += getTD(i + 1);
            html += getTD(v.customerName);
            html += getTD(v.mobile);
            html += getTD(v.pickUpCode);
            html += getTD(v.inTimeString);
            html += getTD(v.outTimeString);
            if (null == v.outTimeString) {
                html += getTD("<button type='button' class='pickup'  expressId='" + v.expressId + "' >取件登记</button>");
            } else {
                html += getTD("");
            }

            html += "</tr>";
        });
        $("#list tr:gt(0)").remove();
        $("#list").append(html);
    };

    /**
     *设置页目条
     * isFirst 是否是首页
     * isLast 是否是尾页
     * nowPage 当前页
     * totalPages 总页数
     **/
    const setPageBar = (isFirst, isLast, nowPage, totalPages) => {
        nowPageNum = nowPage + 1;

        let startPageNum = nowPageNum - 2;
        startPageNum = startPageNum < 1 ? 1 : startPageNum;
        let endPageNum = startPageNum + 4;
        endPageNum = endPageNum > totalPages ? totalPages : endPageNum;

        let html = "";
        if (!isFirst) {
            html += "<span page='1'>&lt;&lt;</span><span page='" + (nowPageNum - 1) + "'>&lt;</span>";
        }
        for (; startPageNum <= endPageNum; startPageNum++) {
            html += "<span page='" + startPageNum + "' class='" + (startPageNum == nowPageNum ? "selected" : "") + "'>" + startPageNum + "</span>";
        }
        if (!isLast) {
            html += "<span page='" + (nowPageNum + 1) + "'>&gt;</span><span  page='" + totalPages + "'>&gt;&gt;</span>";
        }
        $("#pagelist").html(html);
    }

    /**
     *查询
     * param pageNum 页数
     **/
    const search = (pageNum) => {
        let pickUpCode = $("#pickUpCodeSearch").val();
        let mobile = $("#mobileSearch").val();
        let customerName = $("#customerNameSearch").val();
        $.post(Base_Url + "express/listSearch", {
            pickUpCode: pickUpCode,
            mobile: mobile,
            customerName: customerName,
            page: pageNum - 1,
            size: pageSize
        }, (data) => {
            if (data.status == "S") {
                setTableList(data.data.content);
                setPageBar(data.data.first, data.data.last, data.data.number, data.data.totalPages);
            } else {
                alert(data.sysMsg)
            }
        });
    };
    searchBut.on("click", () => {
        search(1)
    });


    /**
     * 打开窗口
     **/
    const openWin = () => {
        $("#mobile").val("");
        $("#customerName").val("");
        $("#win").show();
    }
    /**
     * 关闭窗口
     **/
    const closeWin = () => {
        $("#win").hide();
    }
    addBut.on("click", openWin);
    cancelBut.on("click", closeWin);

    /**
     *提交入库
     **/
    putInStorageBut.on("click", () => {
        let mobile = $("#mobile").val();
        let customerName = $("#customerName").val();
        $.post(Base_Url + "express/enterStore", {
            mobile: mobile,
            customerName: customerName
        }, (data) => {
            if (data.status == "S") {
                closeWin();
                search(1);
            } else {
                alert(data.sysMsg);
            }
        });
    });


    /**
     *绑定出库
     **/
    $(document).on('click', '.pickup', function () {
        var expressId = $(this).attr("expressId");
        $.post(Base_Url + "express/pickUp", {expressId: expressId}, function (data) {
            if (data.status == "S") {
                search(nowPageNum);
            } else {
                alert(data.sysMsg);
            }
        });
    });

    $(document).on('click', '#pagelist span', function () {
        search(parseInt($(this).attr("page")));
    });

    search(1);

});
